package com.xindaoapp.frameworkfragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.xindaoapp.framework.MainActivity;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.utils.ToastUtil;

public class LeftMenuFragment extends Fragment implements OnItemClickListener {

	private View view;
	private List<String> list;
	private ArrayAdapter<String> adapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		ListView listView = (ListView) view.findViewById(R.id.list_view);

		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, android.R.id.text1,
				initData());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	private List<String> initData() {
		list = new ArrayList<String>();
		list.add("标签一");
		list.add("标签二");
		list.add("标签三");
		list.add("标签四");
		list.add("标签五");
		return list;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = LayoutInflater.from(getActivity()).inflate(R.layout.list_view,
				null);
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Fragment f = null;
		switch (position) {
		case 0:
//			f = new Fragment1();
			ToastUtil.showShortToast(getActivity(), "标签一");
			break;
		case 1:
			ToastUtil.showShortToast(getActivity(), "标签二");
//			f = new Fragment2();
			break;
		case 2:
			ToastUtil.showShortToast(getActivity(), "标签三");
//			f = new Fragment3();
			break;
		case 3:
			ToastUtil.showShortToast(getActivity(), "标签四");
//			f = new Fragment4();
			break;
		case 4:
			ToastUtil.showShortToast(getActivity(), "标签五");
//			f = new Fragment5();
			break;
		}
		switchFragment(f);
	}
	
	/**
	 * 
	 1 new MainActivity(); 得到activity的对象 2 对外暴露一个方法，去改变内容页
	 */
	private void switchFragment(Fragment f) {
		if (null != f) {
			if (getActivity() instanceof MainActivity) {
				((MainActivity) getActivity()).switchFragment(f);
			}
		}
	}

}
